/*
 * Copyright (C) 2015 yydcdut (yuyidong2015@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.yydcdut.sdlv;


import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.ListContainer;
import ohos.app.Context;

/**
 * Created by yuyidong on 15/9/30.
 */
class DragListView extends ListContainer {
    /* 监听器 */
    private SlideAndDragListView.OnDragDropListener mOnDragDropListener;
    /* 监听器 */
    private Callback.OnDragDropListener mAdapterDragDropListener;
    private Callback.OnDragDropListener mDragListDragDropListener;
    /* drag */
    public DragManager mDragManager;

    /**
     * y偏离范围
     */
    private int deviationRangeY = 65;

    public DragListView(Context context) {
        this(context, null);
    }

    public DragListView(Context context, AttrSet attrs) {
        this(context, attrs, null);
    }

    public DragListView(Context context, AttrSet attrs, String defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void intConfig(Context context, ComponentContainer decorView) {
        mDragManager = new DragManager(context, this, decorView);
    }


    protected void setDragPosition(int position) {
        Component view = getComponentAt(position);
        LogUtil.error("1-------DragManger------onTouchEvent----event setDragging start");
        if (mOnDragDropListener != null && view instanceof ItemMainLayout) {
            ItemMainLayout itemMainLayout = (ItemMainLayout) getComponentAt(position);
            itemMainLayout.getItemLeftBackGroundLayout().setVisibility(HIDE);
            itemMainLayout.getItemRightBackGroundLayout().setVisibility(HIDE);
            mDragManager.setDragging(true);
            LogUtil.error("1-------DragManger------onTouchEvent----event setDragging");
        }
    }

    /**
     * 设置drag的监听器，加入数据
     *
     * @param onDragDropListener
     */
    public void setOnDragDropListener(SlideAndDragListView.OnDragDropListener onDragDropListener) {
        mOnDragDropListener = onDragDropListener;
    }

    //TODO 这里需要优化 获取的Item索引不准确
    protected Component getViewByPoint(int x, int y) {
        y = y - deviationRangeY;
        int count = getChildCount();
        Component child;
        for (int childIdx = 0; childIdx < count; childIdx++) {
            child = getComponentAt(childIdx);
            if (child != null && y >= child.getTop() && y <= child.getBottom() && x >= child.getLeft() && x <= child.getRight()) {
                return child;
            }
        }
        return null;
    }

    protected void handleDragStarted(int x, int y) {
        Component view = getViewByPoint(x, y);
        if (view == null) {
            return;
        }
        boolean isDragging = false;
        if (mAdapterDragDropListener != null) {
            isDragging = mAdapterDragDropListener.onDragStarted(x, y, view);
        }
        if (mDragListDragDropListener != null && isDragging) {
            mDragListDragDropListener.onDragStarted(x, y, view);
        }
        if (mOnDragDropListener != null && isDragging) {
            mOnDragDropListener.onDragViewStart(getIndexForComponent(view));
        }
    }

    protected void handleDragMoving(int x, int y) {
        Component view = getViewByPoint(x, y);
        if (view == null) {
            return;
        }
        if (mAdapterDragDropListener != null) {
            mAdapterDragDropListener.onDragMoving(x, y, view, mOnDragDropListener);
        }
        if (mDragListDragDropListener != null) {
            mDragListDragDropListener.onDragMoving(x, y, view, null);
        }
    }

    protected void handleSetListener() {
        if (mAdapterDragDropListener != null) {
            mAdapterDragDropListener.onSetListener(mOnDragDropListener);
        }
    }

    protected void handleDragFinished(int x, int y) {
        if (mAdapterDragDropListener != null) {
            mAdapterDragDropListener.onDragFinished(x, y, mOnDragDropListener);
        }
        if (mDragListDragDropListener != null) {
            mDragListDragDropListener.onDragFinished(x, y, null);
        }
    }

    protected void setListDragDropListener(Callback.OnDragDropListener listener) {
        mDragListDragDropListener = listener;
    }

    protected void serAdapterDragDropListener(Callback.OnDragDropListener listener) {
        mAdapterDragDropListener = listener;
    }

    protected boolean isDragging() {
        return mDragManager.isDragging();
    }

    public DragManager getDragManager() {
        return mDragManager;
    }

}
